"""
@Filename:  web_driver/day32_p2p
@Author:  liukai
@Time:  2022/11/22 0022  21:45
"""
import time

import ddddocr
import webdriver_helper
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_helper import debugger

from web_driver.util_p2p import save_cookie, load_cookie, is_login

d = webdriver_helper.get_webdriver()
d.maximize_window()
d.implicitly_wait(3)
d.get("http://47.107.116.139/fangwei/m.php?m=Public&a=login&")

load_cookie(d)
# 未登录执行
if is_login(d):
    # 登录账户密码验证码，点击登录
    d.find_element(By.XPATH, "/html/body/form/table/tbody/tr/td[3]/table/tbody/tr[2]/td[2]/input").send_keys('admin')
    d.find_element(By.XPATH, "/html/body/form/table/tbody/tr/td[3]/table/tbody/tr[3]/td[2]/input").send_keys('msjy123')
    ocr = ddddocr.DdddOcr()
    # 重复获取验证码登录
    for i in range(100):
        d.find_element(By.XPATH, '//*[@id="verify"]').screenshot('./img.png')
        text = ocr.classification(open(r"D:\software1\ui_by_selenium\web_driver\img.png", "rb").read())
        d.find_element(By.XPATH, '/html/body/form/table/tbody/tr/td[3]/table/tbody/tr[5]/td[2]/input').clear()
        d.find_element(By.XPATH, '/html/body/form/table/tbody/tr/td[3]/table/tbody/tr[5]/td[2]/input').send_keys(text)
        d.find_element(By.XPATH, '//*[@id="login_btn"]').click()
        wa = WebDriverWait(d, 5)
        re = wa.until(lambda x: x.find_element(By.XPATH, '//*[@id="login_msg"]').text.strip())
        if re == '登录成功':
            save_cookie(d)
            break
        time.sleep(2)

# 新增一个贷款
if True:
    # 点击贷款管理
    print("查找")
    mode = d.find_element(By.XPATH, "/html/frameset/frame[1]")
    d.switch_to.frame(mode)
    d.find_element(By.XPATH, '//*[@id="navs"]/ul/li[2]/a').click()
    d.switch_to.default_content()
    # 点击全部贷款
    mode = d.find_element(By.XPATH, '//*[@id="menu-frame"]')
    d.switch_to.frame(mode)
    d.find_element(By.XPATH, '/html/body/dl[1]/dd[1]/a').click()
    d.switch_to.default_content()
    # 点击新增贷款
    mode = d.find_element(By.XPATH, '//*[@id="main-frame"]')
    d.switch_to.frame(mode)
    d.find_element(By.XPATH, '/html/body/div[2]/div[3]/input[1]').click()
    # 填新增贷款 表格
    if True:
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[4]/td[2]/input').send_keys('凑钱娶微微，兄弟们冲！')
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[5]/td[2]/input').send_keys('123123')
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[6]/td[2]/input[1]').send_keys('aa')
        time.sleep(1)
        d.find_element(By.XPATH, '/html/body/div[5]/ul/li[2]').click()

        selec = d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[8]/td[2]/select')
        Select(selec).select_by_index(1)
        Select(selec).select_by_visible_text()
        # Select(selec).select_by_index(3)  # 设置下表
        # Select(selec).select_by_value('4')  # 设置value值
        selec = d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[9]/td[2]/select')
        Select(selec).select_by_index(1)

        selec = d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[15]/td[2]/select')
        Select(selec).select_by_index(1)

        # 文件上传
        if True:
            d.find_element(By.XPATH,
                           '/html/body/div[2]/form/table[1]/tbody/tr[14]/td[2]/span/div[1]/div/div/button').click()
            # 点本地文件
            d.find_element(By.XPATH, '/html/body/div[6]/div[1]/div[2]/div/div[1]/ul/li[2]').click()
            # 输入路径
            d.find_element(By.XPATH, '/html/body/div[6]/div[1]/div[2]/div/div[3]/form/div/div/div/input').send_keys(
                r'D:\software1\ui_by_selenium\web_driver\img.png')
            d.find_element(By.XPATH, '/html/body/div[6]/div[1]/div[3]/span[1]/input').click()

        selec = d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[16]/td[2]/select')
        Select(selec).select_by_index(1)

        selec = d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[17]/td[2]/select')
        Select(selec).select_by_index(1)

        selec = d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[18]/td[2]/select')
        Select(selec).select_by_index(1)

        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[19]/td[2]/input').clear()
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[19]/td[2]/input').send_keys('40000')
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[20]/td[2]/input').clear()
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[20]/td[2]/input').send_keys('300')
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[22]/td[2]/input').clear()
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[22]/td[2]/input').send_keys('1')
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[23]/td[2]/input').clear()
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[23]/td[2]/input').send_keys('10000')
        d.find_element(By.XPATH, '//*[@id="repay_time"]').clear()
        d.find_element(By.XPATH, '//*[@id="repay_time"]').send_keys('3')
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[27]/td[2]/input').clear()
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[27]/td[2]/input').send_keys('5')
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[28]/td[2]/input').send_keys('5')

        # 复选框  全国
        fxk = d.find_element(By.XPATH, '//*[@id="citys_box"]/div[1]/div[2]/input[1]')
        d.execute_script('arguments[0].scrollIntoView()', fxk)
        time.sleep(2)
        d.find_element(By.XPATH, '//*[@id="citys_box"]/div[1]/div[2]/input[1]').click()
        d.find_element(By.XPATH, '//*[@id="citys_box"]/div[1]/div[2]/input[2]').click()

        # 借款状态  筹款中
        # 输入开始时间
        time.sleep(2)
        time_ele = d.find_element(By.XPATH, '//*[@id="start_time"]')
        d.execute_script('arguments[0].scrollIntoView()', time_ele)

        d.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[33]/td[2]/label[1]/input').click()
        d.execute_script('arguments[0].value = "2022-11-24 00:00:00"', time_ele)

        # 提交按钮
        d.find_element(By.XPATH, '/html/body/div[2]/form/table[6]/tbody/tr[2]/td[2]/input[4]').click()

        result = d.find_element(By.XPATH, '/html/body/div/table/tbody/tr[3]/td')
        if result.text == '添加成功':
            print('添加成功')
            time.sleep(2)
            d.find_element(By.XPATH, '/html/body/div[2]/div[1]/a').click()
            # 点击无效
            d.find_element(By.XPATH, '//*[@id="dataTable"]/tbody/tr[3]/td[17]/span').click()
        else:
            print('失败', result)

time.sleep(10)
d.quit()

if __name__ == '__main__':
    pass
